package cn.wangxl.loglog.dao;

import cn.wangxl.loglog.constants.Application;
import cn.wangxl.loglog.constants.MongoNameUtil;
import cn.wangxl.loglog.domain.BasicLogDO;
import cn.wangxl.loglog.qo.BasicQuery;
import cn.wangxl.loglog.qo.ErrorLogQuery;
import cn.wangxl.loglog.qo.LogQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.TextCriteria;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

/**
 * mongo dao
 */
@Slf4j
@Repository
public class MongoLogRepository {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 单条数据插入
     * @param basicLogDO
     */
    public void insert(BasicLogDO basicLogDO, Application application, String collectionName){
        mongoTemplate.insert(basicLogDO, MongoNameUtil.getMongoCollectionName(application,collectionName));
    }

    /**
     * 批量数据插入
     * @param basicLogDOList
     */
    public void batchInsert(List<BasicLogDO> basicLogDOList, Application application, String collectionName){
        mongoTemplate.insert(basicLogDOList, MongoNameUtil.getMongoCollectionName(application,collectionName));
    }

    /**
     * 删除单条数据
     * @param id
     */
    public void delete(Integer id, Application application, String collectionName){
        mongoTemplate.remove(query(where("id").is(id)),MongoNameUtil.getMongoCollectionName(application,collectionName));
    }

    /**
     * 使用query对象查询
     * @return
     */
    public List<BasicLogDO> query1(BasicQuery basicQuery, Application application, String collectionName){

        Query query = getQuery(basicQuery);

        long count = mongoTemplate.count(query, LogQuery.class,MongoNameUtil.getMongoCollectionName(application,collectionName));
        if(count == 0){
            return Collections.emptyList();
        }
        List<BasicLogDO> res = mongoTemplate.find(query, BasicLogDO.class, MongoNameUtil.getMongoCollectionName(application,collectionName));
        return res;
    }

    /**
     * 使用query对象查询
     * @return
     */
    public long count1(BasicQuery basicQuery, Application application, String collectionName){

        Query query = getQuery(basicQuery);

        long count = mongoTemplate.count(query, LogQuery.class,MongoNameUtil.getMongoCollectionName(application,collectionName));

        return count;
    }
    /**
     * 使用query()静态方法对象查询
     * @return
     */
    public List<BasicLogDO> query2(BasicQuery basicQuery, Application application, String collectionName){

        Query query = getQuery(basicQuery);
        long count = mongoTemplate.count(query, BasicLogDO.class,MongoNameUtil.getMongoCollectionName(application,collectionName));
        if(count == 0){
            return Collections.emptyList();
        }
        List<BasicLogDO> res = mongoTemplate.find(query, BasicLogDO.class, MongoNameUtil.getMongoCollectionName(application,collectionName));
        return res;
    }

    private Query getQuery(BasicQuery basicQuery){

        Query query = new Query();
        if(Objects.nonNull(basicQuery.getId())){
            query.addCriteria(where("id").is(basicQuery.getId()));
        }
        if(Objects.nonNull(basicQuery.getBegin()) && Objects.isNull(basicQuery.getEnd())){
            query.addCriteria(where("createTime").gte(basicQuery.getBegin()));
        }else if(Objects.isNull(basicQuery.getBegin()) && Objects.nonNull(basicQuery.getEnd())){
            query.addCriteria(where("createTime").lte(basicQuery.getEnd()));
        }else if(Objects.nonNull(basicQuery.getBegin()) && Objects.nonNull(basicQuery.getEnd())){
            query.addCriteria(where("createTime").gte(basicQuery.getBegin()).lte(basicQuery.getEnd()));
        }
        // 全文检索，类似于mysql的like但是效率高
        if(!StringUtils.isEmpty(basicQuery.getMessage())){
            TextCriteria textCriteria = new TextCriteria();
            query.addCriteria(textCriteria.matchingAny(basicQuery.getMessage()));
        }

        return query;
    }

}
